﻿using System;
using System.Collections.Generic;
using Verse;
using Verse.AI;

namespace RI_Living
{
	// Token: 0x02000004 RID: 4
	public class JobDriver_FillRI_Building_SilkwormRearingBox : JobDriver
	{
		// Token: 0x17000009 RID: 9
		// (get) Token: 0x0600001A RID: 26 RVA: 0x000027F0 File Offset: 0x000009F0
		protected Building_Fermentation Barrel
		{
			get
			{
				return (Building_Fermentation)this.job.GetTarget(TargetIndex.A).Thing;
			}
		}

		// Token: 0x1700000A RID: 10
		// (get) Token: 0x0600001B RID: 27 RVA: 0x0000281C File Offset: 0x00000A1C
		protected Thing Wort
		{
			get
			{
				return this.job.GetTarget(TargetIndex.B).Thing;
			}
		}

		// Token: 0x0600001C RID: 28 RVA: 0x00002844 File Offset: 0x00000A44
		public override bool TryMakePreToilReservations(bool errorOnFailed)
		{
			return this.pawn.Reserve(this.Barrel, this.job, 1, -1, null, errorOnFailed, false) && this.pawn.Reserve(this.Wort, this.job, 1, -1, null, errorOnFailed, false);
		}

		// Token: 0x0600001D RID: 29 RVA: 0x0000289D File Offset: 0x00000A9D
		protected override IEnumerable<Toil> MakeNewToils()
		{
			this.FailOnDespawnedNullOrForbidden(TargetIndex.A);
			this.FailOnBurningImmobile(TargetIndex.A);
			base.AddEndCondition(delegate
			{
				bool flag = this.Barrel.SpaceLeftForWort > 0;
				JobCondition result;
				if (flag)
				{
					result = JobCondition.Ongoing;
				}
				else
				{
					result = JobCondition.Succeeded;
				}
				return result;
			});
			yield return Toils_General.DoAtomic(delegate
			{
				this.job.count = this.Barrel.SpaceLeftForWort;
			});
			Toil reserveWort = Toils_Reserve.Reserve(TargetIndex.B, 1, -1, null, false);
			yield return reserveWort;
			yield return Toils_Goto.GotoThing(TargetIndex.B, PathEndMode.ClosestTouch, false).FailOnDespawnedNullOrForbidden(TargetIndex.B).FailOnSomeonePhysicallyInteracting(TargetIndex.B);
			yield return Toils_Haul.StartCarryThing(TargetIndex.B, false, true, false, true, false).FailOnDestroyedNullOrForbidden(TargetIndex.B);
			yield return Toils_Haul.CheckForGetOpportunityDuplicate(reserveWort, TargetIndex.B, TargetIndex.None, true, null);
			yield return Toils_Goto.GotoThing(TargetIndex.A, PathEndMode.Touch, false);
			yield return Toils_General.Wait(200, TargetIndex.None).FailOnDestroyedNullOrForbidden(TargetIndex.B).FailOnDestroyedNullOrForbidden(TargetIndex.A).FailOnCannotTouch(TargetIndex.A, PathEndMode.Touch).WithProgressBarToilDelay(TargetIndex.A, false, -0.5f);
			Toil toil = ToilMaker.MakeToil("MakeNewToils");
			toil.initAction = delegate()
			{
				this.Barrel.AddWort(this.Wort);
			};
			toil.defaultCompleteMode = ToilCompleteMode.Instant;
			yield return toil;
			yield break;
		}

		// Token: 0x04000011 RID: 17
		private const TargetIndex BarrelInd = TargetIndex.A;

		// Token: 0x04000012 RID: 18
		private const TargetIndex WortInd = TargetIndex.B;

		// Token: 0x04000013 RID: 19
		private const int Duration = 200;
	}
}
